package com.erp.erp_mask.controller;

import com.erp.erp_mask.pojo.TCustomers;
import com.erp.erp_mask.service.TCustomersService;
import com.erp.erp_mask.vo.Result;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

@RestController
@RequestMapping("/cust")
public class TCustomersController {

    @Resource
    private TCustomersService tCustomersService;


    @GetMapping("/index")
    public ModelAndView index(){
        return new ModelAndView("/sys/customers");
    }

    @GetMapping("/search")
    public Result searchcus(String name, Long pageNo, Long pageSize){
        return Result.success(tCustomersService.searchcus(name,pageNo,pageSize));
    }

    @PostMapping("/insert")
    public Result insert(@RequestBody TCustomers tCustomers){
        System.out.println(tCustomers);
        try{
            tCustomersService.save(tCustomers);
        return Result.success("新增成功!");
        }catch (Exception e){
            return Result.fail("新增失败!");
        }
    }



    @PostMapping("/update")
    public Result update(@RequestBody TCustomers tCustomers){
        try{
            tCustomersService.updateById(tCustomers);
            return Result.success("客户修改成功！");
        }catch (Exception e){
            return Result.fail("客户修改失败");
        }
    }



    @DeleteMapping("/del")
    public Result del(Integer custId){
        try{
            tCustomersService.removeById(custId);
            return Result.success("客户删除成功！");
        }catch (Exception e){
            return Result.fail("客户删除失败！");
        }
    }
    @GetMapping("ductsearch")
    public Result ductsearch(){
        return Result.success(tCustomersService.list());
    }
}
